package cn.shenlong.portal.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.MemberValid;
import cn.shenlong.portal.mapper.MemberValidMapper;
import cn.shenlong.portal.service.MemberValidService;

@Service("MemberValidService")
public class MemberValidServiceImpl implements MemberValidService {

	@Autowired
	private MemberValidMapper MemberValidMapper;
	
	@Override
	@Transactional
	public void save(MemberValid memberValid,long memberValidId) {
		MemberValidMapper.saveEntity(memberValid);
	}

	@Override
	public Pagination findMemberValid(MemberValid memberValid, Map<String, Object> otherParm,Integer pageSize, Integer pageNo) {
		Pagination p = new Pagination();
		List<MemberValid> contentList= MemberValidMapper.findPageListByCriteria(initmemberValidParm(memberValid,otherParm), initRowBounds(pageSize, pageNo, p));
		Long count = MemberValidMapper.findCountByCriteria(initmemberValidParm(memberValid,otherParm));
		p.setList(contentList);
		p.setTotalCount(count.intValue());
		return p;
	}
	
	private Map<String,Object> initmemberValidParm(MemberValid memberValid,Map<String,Object> otherParm){
		Map<String,Object> parm = new HashMap<String, Object>();
		if(otherParm!=null){
			parm.putAll(otherParm);
		}
		if(memberValid==null){
			return parm;
		}else{
			parm.put("id", memberValid.getId());
			parm.put("code", memberValid.getCode());
			parm.put("memberId", memberValid.getMemberId());
			return parm;
		}

	}
    private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Pagination p) {
        RowBounds rowBounds = null;
        if (pageSize == null || pageNo == null) {
            rowBounds = new RowBounds();
        } else {
            p.setPageNo(pageNo);
            p.setPageSize(pageSize);
            rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
        }
        return rowBounds;
    }

	@Override
	public MemberValid findMemberValidByMemberValidId(long memberValidId) {
		return MemberValidMapper.findByPK(memberValidId);
	}

	@Override
	public MemberValid updateMemberValid(MemberValid memberValid) {
		 int i = MemberValidMapper.updateEntity(memberValid);
		 if(i<0) return null;
		 return MemberValidMapper.findByPK(memberValid.getId());
	}

	@Override
	public void deleteMemberValid(Long[] memberValidId) {
		MemberValidMapper.deleteByPKArray(memberValidId);
	}

	@Override
	public void deleteByMemberValidId(List<Long> MemberValidId) {
		MemberValidMapper.deleteByPKArray((Long[])MemberValidId.toArray());
	}
	
	public void save(MemberValid memberValid){
		MemberValidMapper.saveEntity(memberValid);
	}
}
